package com.timmy.bluetooth;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.timmy.bluetooth.connect.Command;
import com.timmy.bluetooth.utils.DebugLog;
import com.timmy.bluetooth.connect.ILoopTimerListener;
import com.timmy.bluetooth.connect.LoopTimer;
import com.timmy.bluetooth.utils.Mode;
import com.timmy.bluetooth.utils.ShareUtil;

public class ShakeActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "震动啊震动";
    private Button mShakeBtn;
    /**
     * 当前震动强度
     */
    private int saveTime;
    LoopTimer loopTimer;

    private final int POWER_CHANGE = 1;
    private final int CONTROL_CHANGE = 2;
    private final int STOP = 3;
    public static final int TIME_CHANGE = 6;

    Mode cShockPower = Mode.soft;
    Mode cControlMode = Mode.sync;

    private long time;
    private boolean isHasControl;
    private long startTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shake);

        mShakeBtn = (Button) findViewById(R.id.shake_btn);

        mShakeBtn.setOnClickListener(this);
        loopTimer = new LoopTimer(timerListener, 1000);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shake_btn:

                if (isHasControl == false) {

                    //开始震动
                    Log.i(TAG, "onClick: 开始震动");

                    ShareUtil.SaveDeviceState(ShakeActivity.this, true);
                    isHasControl = true;
                    handler.sendEmptyMessage(POWER_CHANGE);
                    handler.sendEmptyMessage(CONTROL_CHANGE);
                    saveTime = 0;
                    loopTimer.startTimer();

                    startTime = System.currentTimeMillis() / 1000;         //获取当前时间
                    ShareUtil.SaveTimeState(ShakeActivity.this, true);       //保存当前时间
                } else {
                    //停止震动
                    Log.i(TAG, "onClick: 停止震动");
                    isHasControl = false;

                    ShareUtil.SaveDeviceState(ShakeActivity.this, false);

                    time = 0;
                    handler.sendEmptyMessage(STOP);
                }
                break;
        }
    }

    ILoopTimerListener timerListener = new ILoopTimerListener() {
        @Override
        public void onLoopTimer() {

            if (isHasControl) {
                time++;
            } else {
                time = 0;
            }

            saveTime++;
            Message message = new Message();
            message.what = TIME_CHANGE;
            message.obj = time;
            handler.sendMessage(message);
            //if (time % 2 != 0) {
            //handler.sendEmptyMessage(UI_CHANGE);
            //} else {
            //handler.sendEmptyMessage(UI_CHANGE_1);
            //}
        }
    };

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case POWER_CHANGE: //控制震动频率的
                    updateCommand((byte) cShockPower.getValue());
                    break;
                case CONTROL_CHANGE:            //控制开关的  0为右胸震动关闭,1为开启,2为左胸震动关闭
                    byte value = (byte) cControlMode.getValue();
                    Log.i(TAG, "handleMessage: " + value);
                    Command.getInstance().buildCommandByType(Command.CommandType.CONTROL_STYLE, (byte) 1);

                    break;
                case STOP:
                    DebugLog.e("停止计时");
                    updateCommand(Command.NC);
                    loopTimer.stopTimer();
                    time = 0;
                    break;
                case TIME_CHANGE:           //6
                    //int currentTime = (int) msg.obj;
                    break;
            }
        }
    };

    private void updateCommand(byte value) {
        Command.getInstance().buildCommandByType(Command.CommandType.SHOCK_FUNCION, value);

        Log.i("啦啦啦啦啦", "updateCommand: " + value);

        if (value == Command.NC) {
            Log.i("啦啦啦啦啦", "updateCommand: 进入了判断 " + value);
            Command.getInstance().buildCommandByType(Command.CommandType.CONTROL_STYLE, (byte) Mode.sync.getValue());
        }
    }
}
